package com.qf.order.controller;

import com.alibaba.excel.EasyExcel;
import com.qf.common.annotation.Anonymous;
import com.qf.common.core.controller.BaseController;
import com.qf.common.core.domain.AjaxResult;
import com.qf.order.domain.GoodsInfo;
import com.qf.order.domain.dto.OrderExcelDTO;
import com.qf.order.domain.OrderQueryParams;
import com.qf.order.domain.OrderVO;
import com.qf.order.listener.OrderImportListener;
import com.qf.order.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/tms/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderServiceImpl orderService;


    /**
     * 生成订单编号
     *
     * @return
     */
    @GetMapping("/requestOrderId")
    @Anonymous
    public AjaxResult requestOrderId() {
        String orderId = orderService.requestOrderId();
        return AjaxResult.success(orderId);
    }

    /**
     * 接收订单
     * @return
     */
    @PostMapping("/add")
    @Anonymous
    public AjaxResult add(@RequestBody OrderVO orderVO) {
        //打印orderVO 对象
        System.out.println(orderVO.getGoodsList());
        //service层处理数据
        orderService.add(orderVO);
        //返回结果
        return AjaxResult.success();
    }

    /**
     * 查询订单列表
     * @return
     */
    @GetMapping("/list")
    @Anonymous
    public AjaxResult list(OrderVO orderVO) {
        //service层处理数据
        List<OrderVO> orderVOList = orderService.selectOrderList(orderVO);
        return AjaxResult.success(orderVOList);
    }
    /**
     * 取消订单
     * @return
     */
    @DeleteMapping("/{orderIds}")
    @Anonymous
    public AjaxResult remove(@PathVariable String[] orderIds) {
        //service层处理数据
        orderService.delOrders(orderIds);
        return AjaxResult.success();
    }

    /**
     * 更新订单
     */
    @PutMapping("/update")
    @Anonymous
    public AjaxResult update(@RequestBody OrderVO orderVO) {
        System.out.println(orderVO);
        return AjaxResult.success(orderService.update(orderVO));
    }

    /**
     * 订单详情
     */
    @GetMapping("/orderInfo/{orderId}")
    @Anonymous
    public AjaxResult orderInfo(@PathVariable String orderId) {
        OrderVO orderVO = orderService.getOrderInfo(orderId);
        return AjaxResult.success(orderVO);
    }

    /**
     * 订单详情页删除货物信息
     */
    @GetMapping("/delGoods")
    @Anonymous
    public AjaxResult delGoods(GoodsInfo goodsInfo) {

        return toAjax(orderService.delGoods(goodsInfo));

    }

    /**
     * 导出订单数据（支持选中导出和筛选导出）
     */
    @GetMapping("/export")
    public void exportOrders(
            // 接收选中的订单ID列表（多个ID）
            @RequestParam(required = false) List<String> orderIds,
            // 筛选条件参数
            OrderQueryParams params,
            HttpServletResponse response
    ) throws Exception {
        // 调用服务层，传递选中的ID列表和筛选参数
        orderService.exportOrders(orderIds, params, response);
    }


    /**
     * 导入订单数据
     */

    @PostMapping("/import")
    public String importOrders(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return "文件不能为空";
        }

        // 读取Excel并处理
        EasyExcel.read(file.getInputStream(), OrderExcelDTO.class, new OrderImportListener(orderService))
                .sheet()
                .doRead();

        return "导入成功，共导入" + file.getSize() + "字节数据";
    }

    /**
     * 查询顾客信息
     */

    @GetMapping("/customerInfo")
    @Anonymous
    public AjaxResult customerInfo() {
        return AjaxResult.success(orderService.customerInfo());

    }

}
